package com.wms.service.impl;

import com.wms.entity.BaseWarehouse;
import com.wms.entity.BaseWarehouseDTO;
import com.wms.mapper.BaseWarehouseMapper;
import com.wms.service.BaseWarehouseService;
import com.wms.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author TianWenHe
 * @date 2025/4/28
 * @desc
 */
@Service
public class BaseWarehouseServiceImpl implements BaseWarehouseService {
    @Autowired
    private BaseWarehouseMapper baseWarehouseMapper;
    private static final String IMAGE_PREFIX = "https://jinghaiyang.oss-cn-beijing.aliyuncs.com/app/upload/";

    @Override
    public List<BaseWarehouse> find(BaseWarehouseDTO baseWarehouseDTO) {
        return baseWarehouseMapper.find(baseWarehouseDTO);
    }

    @Override
    public Long add(BaseWarehouse baseWarehouse) {
        // 处理图片URL前缀
        if (baseWarehouse.getImage() != null && baseWarehouse.getImage().startsWith(IMAGE_PREFIX)) {
            baseWarehouse.setImage(baseWarehouse.getImage().replace(IMAGE_PREFIX, ""));
        }
        // 统一设置创建时间
        baseWarehouse.setCreateTime(DateUtils.addFixedEightHours(new Date()));

        baseWarehouseMapper.add(baseWarehouse);
        return baseWarehouse.getWarehouseId();
    }

    @Override
    public void update(BaseWarehouse baseWarehouse) {
        // 处理图片URL前缀
        if (baseWarehouse.getImage() != null && baseWarehouse.getImage().startsWith(IMAGE_PREFIX)) {
            baseWarehouse.setImage(baseWarehouse.getImage().replace(IMAGE_PREFIX, ""));
        }
        // 统一设置更新时间
        baseWarehouse.setUpdateTime(DateUtils.addFixedEightHours(new Date()));

        baseWarehouseMapper.update(baseWarehouse);
    }

    @Override
    public void delete(List<Long> ids) {
        baseWarehouseMapper.delete(ids);
    }

    @Override
    public void insertBatch(List<BaseWarehouse> baseWarehouses) {
        // 统一设置创建时间
        for (BaseWarehouse baseWarehouse : baseWarehouses) {
            baseWarehouse.setCreateTime(DateUtils.addFixedEightHours(new Date()));
        }
        baseWarehouseMapper.insertBatch(baseWarehouses);
    }

    @Override
    public List<String> findName() {
        return baseWarehouseMapper.findName();
    }
}
